package view.bieudoindex;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.CriteriaClauses;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

import view.common.Utils;

public class bieudoBean {
    private String sanid;
    private int minVnIndex = 0;
    private static String indexView = "HNXIndexView1Iterator";

    public bieudoBean() {
        super();
    }

    public String getSanid() {
        DCIteratorBinding iterSanck =
            Utils.getIteratorBinding("DmSanCkView1Iterator");
        if (iterSanck.getCurrentRow() != null) {
            sanid = iterSanck.getCurrentRow().getAttribute("Id").toString();
        }
        return sanid;
    }

    public int getMinVnIndex() {
        DCIteratorBinding IteratorHose = Utils.getIteratorBinding(indexView);
        if (IteratorHose.getAllRowsInRange().length > 0) {
            int length = IteratorHose.getAllRowsInRange().length;
            for (int i = 0; i < length; i++) {
                double chiso =
                    Double.valueOf(IteratorHose.getRowAtRangeIndex(i).getAttribute("ChiSo").toString());
                if (i == 0) {
                    chiso = Math.floor(chiso);
                    minVnIndex = (int)chiso;
                }
                if (minVnIndex > chiso) {
                    minVnIndex = (int)chiso;
                }
            }
        }
        if (minVnIndex > 10) {
            minVnIndex = minVnIndex - 5;
        }
        return minVnIndex;
    }

    public void bieudoValueChanged(ValueChangeEvent valueChangeEvent) {
        Utils.setElExpression("#{bindings.DmSanCkView1.inputValue}", valueChangeEvent.getNewValue());
        String san = getSanid();
        if (san.equals("1")) {
            indexView = "HOSEIndexView1Iterator";
        }
        if (san.equals("2")) {
            indexView = "HNXIndexView1Iterator";
        }
    }
}
